package org.mclll.xwangplugin.town.command.executor;

import org.apache.commons.lang.StringUtils;
import org.black_ixx.bossshop.lib.nbtapi.NBT;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.mclll.xwangplugin.town.database.domain.TownDO;
import org.mclll.xwangplugin.town.database.domain.TownShopDO;
import org.mclll.xwangplugin.town.database.mapper.manage.TownManage;
import org.mclll.xwangplugin.town.database.mapper.manage.TownShopManage;

import java.util.Collections;
import java.util.List;
import java.util.UUID;

import static org.mclll.xwangplugin.town.config.TownContext.*;

public class TownShopAddExecutor {
    /**
     * 城镇商店 - shopAdd <城镇名> <数字：物品金币单价> <整数：花费金币一次性最多购买物品数量> <整数：心之石购买物品数量> <整数：花费心之石数量>
        shopAdd
     */

    public static List<String> townShopAddTabComplete(CommandSender sender, String[] args) {
        switch (args.length) {
            case 2:
                return TownManage.selectList(null).stream()
                        .map(TownDO::getName)
                        .filter(townName -> townName.startsWith(args[1]))
                        .collect(java.util.stream.Collectors.toList());
            case 3:
                return Collections.singletonList("<数字：物品金币单价>");
            case 4:
                return Collections.singletonList("<整数：花费金币一次性最多购买物品数量>");
            case 5:
                return Collections.singletonList("<整数：心之石购买数量>");
            case 6:
                return Collections.singletonList("<整数：花费心之石数量>");
            default:
                return null;
        }
    }

    public static void townShopAdd(Player player, String[] args) {
        if (args.length != 6) {
            player.sendMessage(TOWN_SHOP_ADD_COMMAND_FORMAT);
            return;
        }
        ItemStack itemInMainHand = player.getInventory().getItemInMainHand();
        if (itemInMainHand.getType() == Material.AIR) {
            player.sendMessage(ITEM_NOT_FOUND_MAIN_HAND);
            return;
        }
        String rewardJson = NBT.itemStackToNBT(itemInMainHand.asOne()).toString();
        String townName = args[1];
        TownDO townSelectParam = new TownDO();
        townSelectParam.setName(townName);
        List<TownDO> townList = TownManage.selectList(townSelectParam);
        if (townList.isEmpty()) {
            player.sendMessage(TOWN_NOT_FOUND);
            return;
        }
        TownDO town = townList.get(0);
        Double coinGetPerPrice = null;
        try {
            coinGetPerPrice = Double.valueOf(args[2]);
        } catch (NumberFormatException e) {
            player.sendMessage(String.format(NUMBER_PARSE_ERROR, args[2]));
            return;
        }
        if (!StringUtils.isNumeric(args[3])) {
            player.sendMessage(String.format(NUMBER_PARSE_ERROR, args[3]));
            return;
        }
        Integer coinGetCount = Integer.valueOf(args[3]);
        if (!StringUtils.isNumeric(args[4])) {
            player.sendMessage(String.format(NUMBER_PARSE_ERROR, args[4]));
            return;
        }
        Integer perHeartStoneGetCount = Integer.valueOf(args[4]);
        if (!StringUtils.isNumeric(args[5])) {
            player.sendMessage(String.format(NUMBER_PARSE_ERROR, args[5]));
            return;
        }
        Integer perHeartStoneGetPrice = Integer.valueOf(args[5]);
        TownShopDO insert = new TownShopDO();
        insert.setUuid(UUID.randomUUID().toString());
        insert.setTownUuid(town.getUuid());
        insert.setCoinGetCount(coinGetCount);
        insert.setRewardJson(rewardJson);
        insert.setCoinGetPerPrice(coinGetPerPrice);
        insert.setPerHeartStoneGetCount(perHeartStoneGetCount);
        insert.setPerHeartStoneGetPrice(perHeartStoneGetPrice);
        TownShopManage.insert(insert);
        player.sendMessage(String.format(TOWN_SHOP_ADD_SUCCESS, townName));
    }

}
